﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Biz;

namespace control.Dal
{
    class DalSanPham
    {
        internal static List<control.Biz.BizSanPham> GetAll()
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           select new BizSanPham(sp.ID,sp.MaSanPham,sp.TenSanPham,sp.ThongTinSanPham,sp.HinhAnh,(int)sp.SoLuong,(decimal)sp.GiaBan,new BizLoaiSanPham(sp.LoaiSanPham.ID,sp.LoaiSanPham.MaLoai,sp.LoaiSanPham.TenLoai),(bool)sp.IsNew);
           return sanphams.ToList<BizSanPham>();
        }

        internal static BizSanPham GetSanPhamByID(int _ID)
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           where sp.ID==_ID
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            List<BizSanPham> sps= sanphams.ToList<BizSanPham>();
            return sps[0];
        }

        internal static List<BizSanPham> GetNewSp()
        {
            var newsps = from sp in DalCommon.TrangSucDB.SanPhams
                         where sp.IsNew==true
                         select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            return newsps.ToList<BizSanPham>();
        }

        internal static List<BizSanPham> GetSanPhamByIDLoai(int _IDLoai)
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           where sp.IDLoaiSanPham==_IDLoai
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            return sanphams.ToList<BizSanPham>();
        }

        internal static int Insert(BizSanPham _sp)
        {
            SanPham sp = new SanPham()
            {

                MaSanPham = _sp.MaSanPham,
                TenSanPham = _sp.TenSanPham,
                IDLoaiSanPham = _sp.LoaiSanPham.ID,
                SoLuong = _sp.SoLuong,
                GiaBan = _sp.GiaBan,
                HinhAnh = _sp.Hinh,
                IsNew = _sp.IsNew,
                ThongTinSanPham = _sp.ThongTinSanPham,
            };
            DalCommon.TrangSucDB.SanPhams.InsertOnSubmit(sp);
            DalCommon.TrangSucDB.SubmitChanges();
            return sp.ID;
        }

        internal static bool Delete(BizSanPham bizSanPham)
        {
            try
            {
                SanPham sp = DalCommon.TrangSucDB.SanPhams.Single(p => p.ID == bizSanPham.ID);
                DalCommon.TrangSucDB.SanPhams.DeleteOnSubmit(sp);
                DalCommon.TrangSucDB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        internal static BizSanPham GetByID(int ID)
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           where sp.ID==ID
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            return sanphams.FirstOrDefault<BizSanPham>();
        }

        internal static bool Update(BizSanPham bizSanPham)
        {
            try
            {
                SanPham sp = DalCommon.TrangSucDB.SanPhams.Single(p => p.ID == bizSanPham.ID);
                sp.MaSanPham=bizSanPham.MaSanPham;
                sp.TenSanPham=bizSanPham.TenSanPham;
                sp.IDLoaiSanPham=bizSanPham.LoaiSanPham.ID;
                sp.GiaBan=bizSanPham.GiaBan;
                sp.HinhAnh=bizSanPham.Hinh;
                sp.ThongTinSanPham=bizSanPham.ThongTinSanPham;
                sp.SoLuong=bizSanPham.SoLuong;
                sp.IsNew = bizSanPham.IsNew;
                DalCommon.TrangSucDB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        internal static List<BizSanPham> Find(string loai, string _sp)
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           where (sp.MaSanPham.Contains(_sp)||sp.TenSanPham.Contains(_sp))&&sp.LoaiSanPham.MaLoai.StartsWith(loai)
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            return sanphams.ToList<BizSanPham>();
        }

        internal static List<BizSanPham> Getramdom(int p)
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            List<BizSanPham> sps= sanphams.ToList<BizSanPham>();
            List<BizSanPham> resuilts = new List<BizSanPham>();
            Random ran = new Random();
            for (int i = 0; i < p; i++)
            {
                resuilts.Add(sps[ran.Next(sps.Count())]);
            }
            return resuilts;
        }

        internal static List<BizSanPham> Getfeature()
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           orderby sp.SoLuong descending
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
           return  sanphams.ToList<BizSanPham>();
        }

        internal static List<BizSanPham> Find(string loai, string _sp, int giatu, int giaden)
        {
            var sanphams = from sp in DalCommon.TrangSucDB.SanPhams
                           where (sp.MaSanPham.Contains(_sp) || sp.TenSanPham.Contains(_sp)) && sp.LoaiSanPham.MaLoai.StartsWith(loai) && sp.GiaBan<=giaden &&sp.GiaBan>=giatu
                           select new BizSanPham(sp.ID, sp.MaSanPham, sp.TenSanPham, sp.ThongTinSanPham, sp.HinhAnh, (int)sp.SoLuong, (decimal)sp.GiaBan, new BizLoaiSanPham(sp.LoaiSanPham.ID, sp.LoaiSanPham.MaLoai, sp.LoaiSanPham.TenLoai), (bool)sp.IsNew);
            return sanphams.ToList<BizSanPham>();
        }
    }
}
